package cn.harry.cabinet.arcface.util;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.arcsoft.face.ErrorInfo;

/**
 * ArcSoft库版本检查工具类
 * 用于检查库版本兼容性和系统信息
 */
public class ArcSoftVersionChecker {
    private static final String TAG = "ArcSoftVersionChecker";
    
    /**
     * 检查ArcSoft库版本兼容性
     */
    public static boolean checkCompatibility(Context context) {
        try {
            Log.i(TAG, "=== ArcSoft库兼容性检查开始 ===");
            
            // 检查Android版本
            checkAndroidVersion();
            
            // 检查设备架构
            checkDeviceArchitecture();
            
            // 检查ArcSoft库版本
            checkArcSoftVersion();
            
            // 检查内存配置
            checkMemoryConfiguration();
            
            Log.i(TAG, "=== ArcSoft库兼容性检查完成 ===");
            return true;
            
        } catch (Exception e) {
            Log.e(TAG, "ArcSoft库兼容性检查失败", e);
            return false;
        }
    }
    
    /**
     * 检查Android版本兼容性
     */
    private static void checkAndroidVersion() {
        int sdkInt = Build.VERSION.SDK_INT;
        String release = Build.VERSION.RELEASE;
        String codename = Build.VERSION.CODENAME;
        
        Log.i(TAG, "Android版本信息:");
        Log.i(TAG, "  SDK版本: " + sdkInt);
        Log.i(TAG, "  发布版本: " + release);
        Log.i(TAG, "  代号: " + codename);
        
        // Android 5.1 (API 22) 兼容性检查
        if (sdkInt == Build.VERSION_CODES.LOLLIPOP_MR1) {
            Log.i(TAG, "  检测到Android 5.1系统，需要特别注意内存管理");
        } else if (sdkInt < Build.VERSION_CODES.LOLLIPOP) {
            Log.w(TAG, "  警告: Android版本过低，可能不支持ArcSoft库");
        } else {
            Log.i(TAG, "  Android版本兼容");
        }
    }
    
    /**
     * 检查设备架构
     */
    private static void checkDeviceArchitecture() {
        String arch = System.getProperty("os.arch");
        String abi = Build.CPU_ABI;
        String abi2 = Build.CPU_ABI2;
        
        Log.i(TAG, "设备架构信息:");
        Log.i(TAG, "  系统架构: " + arch);
        Log.i(TAG, "  主ABI: " + abi);
        Log.i(TAG, "  次ABI: " + abi2);
        
        // 检查是否支持ArcSoft库的架构
        if ("arm64-v8a".equals(abi) || "armeabi-v7a".equals(abi)) {
            Log.i(TAG, "  架构兼容ArcSoft库");
        } else {
            Log.w(TAG, "  警告: 架构可能不兼容ArcSoft库");
        }
    }
    
    /**
     * 检查ArcSoft库版本
     */
    private static void checkArcSoftVersion() {
        try {
            // 尝试获取ArcSoft库版本信息
            Log.i(TAG, "ArcSoft库版本信息:");
            
            // 检查FaceEngine类是否可用
            Class<?> faceEngineClass = Class.forName("com.arcsoft.face.FaceEngine");
            Log.i(TAG, "  FaceEngine类加载成功");
            
            // 检查ImageUtil类是否可用
            Class<?> imageUtilClass = Class.forName("com.arcsoft.imageutil.ArcSoftImageUtil");
            Log.i(TAG, "  ImageUtil类加载成功");
            
            // 检查错误码常量
            Log.i(TAG, "  错误码常量:");
            Log.i(TAG, "    MOK: " + ErrorInfo.MOK);
            Log.i(TAG, "    MERR_BASIC_BASE: " + ErrorInfo.MERR_BASIC_BASE);
            Log.i(TAG, "    MERR_INVALID_PARAM: " + ErrorInfo.MERR_INVALID_PARAM);
            Log.i(TAG, "    MERR_UNSUPPORTED: " + ErrorInfo.MERR_UNSUPPORTED);
            
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "ArcSoft库类加载失败", e);
        } catch (Exception e) {
            Log.e(TAG, "检查ArcSoft库版本时出错", e);
        }
    }
    
    /**
     * 检查内存配置
     */
    private static void checkMemoryConfiguration() {
        Runtime runtime = Runtime.getRuntime();
        long maxMemory = runtime.maxMemory();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        
        Log.i(TAG, "内存配置信息:");
        Log.i(TAG, "  最大堆内存: " + (maxMemory / 1024 / 1024) + "MB");
        Log.i(TAG, "  当前堆内存: " + (totalMemory / 1024 / 1024) + "MB");
        Log.i(TAG, "  可用堆内存: " + (freeMemory / 1024 / 1024) + "MB");
        
        // 检查内存是否足够
        long availableMemory = maxMemory - (totalMemory - freeMemory);
        if (availableMemory < 200 * 1024 * 1024) { // 200MB
            Log.w(TAG, "  警告: 可用内存不足200MB，可能影响ArcSoft库性能");
        } else {
            Log.i(TAG, "  内存配置充足");
        }
    }
    
    /**
     * 获取详细的系统信息
     */
    public static String getSystemInfo() {
        StringBuilder info = new StringBuilder();
        info.append("=== 系统信息 ===\n");
        info.append("Android版本: ").append(Build.VERSION.RELEASE).append(" (API ").append(Build.VERSION.SDK_INT).append(")\n");
        info.append("设备型号: ").append(Build.MODEL).append("\n");
        info.append("制造商: ").append(Build.MANUFACTURER).append("\n");
        info.append("架构: ").append(Build.CPU_ABI).append("\n");
        info.append("内核版本: ").append(System.getProperty("os.version")).append("\n");
        
        Runtime runtime = Runtime.getRuntime();
        info.append("最大内存: ").append(runtime.maxMemory() / 1024 / 1024).append("MB\n");
        info.append("可用内存: ").append((runtime.maxMemory() - (runtime.totalMemory() - runtime.freeMemory())) / 1024 / 1024).append("MB\n");
        
        return info.toString();
    }
} 